import utils from "@common/utils/utils";

export const genFormConfig = (config = {}) => {
	let formConfig = $.extend({}, config);
	if (!utils.isNotEmptyList(formConfig.items) && formConfig.itemField) {
		let itemField = $.extend({}, formConfig.itemField);
		let itemProps = {}, itemWidget = {};
		if (formConfig.itemProps) {
			itemProps = $.extend({}, formConfig.itemProps);
		}
		if (formConfig.itemWidget) {
			itemWidget = $.extend({}, formConfig.itemWidget);
		}
		let items = Object.keys(itemField).map(field => {
			let widget = {
				name: 'ElInput',
			};
			if (itemProps[field] && itemProps[field].widget) {
				widget = itemProps[field].widget;
			}
			if (itemWidget[field]) {
				widget = itemWidget[field];
			}
			let props = $.extend({}, (itemProps[field] || {}));
			props.prop = field;
			props.label = itemField[field];
			props.widget = widget;
			return props;
		});
		delete formConfig.itemField;
		if (typeof formConfig.procItems === 'function') {
			items = formConfig.procItems(items);
		}
		formConfig.items = items;
	}
	return formConfig;
};